package cn.wu.reggie.controller;


import cn.wu.reggie.common.R;
import cn.wu.reggie.dto.DishDto;
import cn.wu.reggie.entity.Category;
import cn.wu.reggie.entity.Dish;
import cn.wu.reggie.query.DishQuery;
import cn.wu.reggie.service.ICategoryService;
import cn.wu.reggie.service.IDishDtoService;
import cn.wu.reggie.service.IDishService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RequestMapping("dish")
@RestController
public class DishController {


    @Autowired
    private IDishService dishService;


    @PostMapping
    public R saveDishWithFlavor(@RequestBody DishDto dishDto, HttpSession session){
        // 1 获取登录的用户id 设置创建人和修改人
        Long userId = (Long) session.getAttribute("employee");
        dishDto.setUpdateUser(userId);
        dishDto.setCreateUser(userId);

        // 2 保存操作
        return dishService.saveDishWithFlavor(dishDto);
    }

    @GetMapping("page")
    public R findByPage(DishQuery qo){

        return dishService.findByPage(qo);
    }

    @GetMapping("{id}")
    public R findById(@PathVariable Long id){
        return dishService.findById(id);
    }

    @PutMapping
    public R update(@RequestBody DishDto dishDto,HttpSession session){
        Long userId = (Long) session.getAttribute("employee");
        dishDto.setUpdateUser(userId);

        return dishService.updateDishWithFlavor(dishDto);
    }

    @GetMapping("/list")
    public R list(Long categoryId){
        List<Dish> list = dishService.getBaseMapper().selectList(
                new QueryWrapper<Dish>().eq("category_id", categoryId));
        return R.success(list);

    }

}
